package com.server.questionbank.snframe.util.FileUtil.common.util;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 内容类型工具类
 */
public final class ContentTypeUtils {
    private static final Map<String, String> CONTENT_TYPES = new HashMap<>();
    
    static {
        // 图片
        CONTENT_TYPES.put("png", "image/png");
        CONTENT_TYPES.put("jpg", "image/jpeg");
        CONTENT_TYPES.put("jpeg", "image/jpeg");
        CONTENT_TYPES.put("gif", "image/gif");
        // 文档
        CONTENT_TYPES.put("pdf", "application/pdf");
        CONTENT_TYPES.put("doc", "application/msword");
        CONTENT_TYPES.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        // 其他
        CONTENT_TYPES.put("zip", "application/zip");
        CONTENT_TYPES.put("rar", "application/x-rar-compressed");
    }
    
    private ContentTypeUtils() {}
    
    public static String getContentType(String filename) {
        if (StringUtils.isBlank(filename)) {
            return "application/octet-stream";
        }
        
        String extension = StringUtils.substringAfterLast(filename, ".");
        return CONTENT_TYPES.getOrDefault(extension.toLowerCase(), "application/octet-stream");
    }
}